package yl.hs.bmipfull.service.equipment.impl;

import org.springframework.beans.factory.annotation.Autowired;
import yl.hs.bmipfull.annotations.DBTable;
import yl.hs.bmipfull.daos.equipment.IEquipmentLogDao;
import yl.hs.bmipfull.daos.flow.IFlowUserTaskDao;
import yl.hs.bmipfull.pojo.equipment.EquipmentLogEntity;
import yl.hs.bmipfull.pojo.flow.FlowUserTaskEntity;
import yl.hs.bmipfull.service.IBaseService;
import yl.hs.bmipfull.service.impl.BaseService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.datastate.ActionType;

import java.util.Date;

/*
 * EquipmentBaseService
 * @category
 * @author 谢景广
 * @date 2022-11-14
 */
public class EquipmentBaseService extends BaseService implements IBaseService {

    protected IEquipmentLogDao logDao;

    @Autowired
    public void setLogDao(IEquipmentLogDao logDao) {
        this.logDao = logDao;
    }

    @Override
    public Integer writeLog(ActionType action, String dataSN, String userContent, String dataJson, Class<?> classType) {
        var user = HsApp.getCurrent();
        var log = new EquipmentLogEntity();
        var userSN = HsApp.getClient();
        String userName = userSN;
        if (user != null) {
            userSN = user.getUserSN();
            userName = user.getUserName();
        }
        String objectName = "信息";
        DBTable dbTable = null;
        if (classType != null) {
            dbTable = classType.getAnnotation(DBTable.class);
        }
        if (dbTable != null) {
            objectName = dbTable.disName();
            log.setTypeCode(dbTable.mapCode());
            log.setTypeName(dbTable.disName());
        }
        else {
            log.setTypeName("系统写入");
            log.setTypeCode("SystemAutoWrite");
        }
        log.setUserSN(userSN);
        var content="";
        switch (action) {
            case PRINT -> content = String.format("%s打印了编号为%s的%s信息", userName, dataSN, objectName);
            case CREATE -> content = String.format("%s创建了编号为%s的%s信息", userName, dataSN, objectName);
            case UPDATE -> content = String.format("%s更新了编号为%s的%s信息:%s", userName, dataSN, objectName, content);
            case DELETE -> content = String.format("%s删除了编号为%s的%s信息", userName, dataSN, objectName);
            case EXPORT -> content = String.format("%s导出了编号为%s的%s信息", userName, dataSN, objectName);
            case IMPORT -> content = String.format("%s导入了编号为%s的%s信息", userName, dataSN, objectName);
            case QUERY -> content = String.format("%s查询了编号为%s的%s信息", userName, dataSN, objectName);
            case UNREVIEWED -> content = String.format("%s撤销审核编号为%s的%s信息", userName, dataSN, objectName);
            case REVIEWED -> content = String.format("%s审核了编号为%s的%s信息", userName, dataSN, objectName);
            default -> content = String.format("%s操作了编号为%s的%s信息", userName, dataSN, objectName);
        }
        log.setUserName(userName);
            log.setContent(content);
        log.setDataSN(dataSN);
        log.setActionType(action.code);
        log.setHandleTime(new Date());
        return logDao.insertNew(log);
    }

    @Override
    public Integer writeLogs(ActionType action, String dataSN, String content, String dataJson, Class<?> classType) {
        return null;
    }

    public Integer writeLog(ActionType action, String dataSN, String userContent, Class<?> classType) {
        var user = HsApp.getCurrent();
        var log = new EquipmentLogEntity();
        var userSN = HsApp.getClient();
        String userName = userSN;
        if (user != null) {
            userSN = user.getUserSN();
            userName = user.getUserName();
        }
        String objectName = "信息";
        DBTable dbTable = null;
        if (classType != null) {
            dbTable = classType.getAnnotation(DBTable.class);
        }
        if (dbTable != null) {
            objectName = dbTable.disName();
            log.setTypeCode(dbTable.mapCode());
            log.setTypeName(dbTable.disName());
        }
        else {
            log.setTypeName("系统写入");
            log.setTypeCode("SystemAutoWrite");
        }
        log.setUserSN(userSN);

        log.setUserName(userName);
        log.setContent(String.format(userContent,userName));
        log.setDataSN(dataSN);
        log.setActionType(action.code);
        log.setHandleTime(new Date());
        return logDao.insertNew(log);
    }
    protected void addTask(String inspectionSN,String type,String userSN,String userName,String processSN){
        var task = new FlowUserTaskEntity();
        task.setSn(queryNewSN(FlowUserTaskEntity.class));
        task.setCreateTime(new Date());
        task.setObjectSN(inspectionSN);
        task.setDataType(type);
        task.setDataCode("equipment_inspection");
        task.setUserSN(userSN);
        task.setUserName(userName);
        task.setTaskState((short)0);
        task.setHandlerUserSN(processSN);
        task.setFlowSN("");
        task.setFlowDataSN("");
        task.setFlowNodeSN("");
        userTaskDao.insertNew(task);
    }
    IFlowUserTaskDao userTaskDao;

    @Autowired
    public void setUserTaskDao(IFlowUserTaskDao userTaskDao) {
        this.userTaskDao = userTaskDao;
    }
    @Override
    public void forWriteLog(String[] snList, short state) {
        var user = HsApp.getCurrent();
        if (state == -2) {
            for (var str:snList) {
                writeLog(ActionType.UPDATE,str,user.getUserName()+"彻底删除了数据编号为"+str+"的数据信息","",null);
            }
        }else{
            for (var str:snList) {
                writeLog(ActionType.UPDATE,str,user.getUserName()+"恢复删除了数据编号为"+str+"的数据信息","",null);
            }
        }
    }
}
